@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">编辑用户</h4>
                </div>

                <div class="card-body">
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form action="{{ route('users.update', $user) }}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <input type="text" class="form-control" id="username" name="username" 
                                   value="{{ old('username', $user->username) }}" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">角色</label>
                            <div class="row">
                                @foreach($roles as $role)
                                <div class="col-md-4">
                                    <div class="form-check">
                                        <input type="checkbox" class="form-check-input" 
                                               name="roles[]" value="{{ $role->id }}"
                                               id="role{{ $role->id }}"
                                               {{ $user->roles->contains($role->id) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="role{{ $role->id }}">
                                            {{ $role->name }}
                                        </label>
                                    </div>
                                </div>
                                @endforeach
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">状态</label>
                            <div class="form-check">
                                <input type="radio" class="form-check-input" name="status" 
                                       value="1" id="status1" {{ $user->status ? 'checked' : '' }}>
                                <label class="form-check-label" for="status1">启用</label>
                            </div>
                            <div class="form-check">
                                <input type="radio" class="form-check-input" name="status" 
                                       value="0" id="status0" {{ !$user->status ? 'checked' : '' }}>
                                <label class="form-check-label" for="status0">禁用</label>
                            </div>
                        </div>

                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">保存修改</button>
                            <a href="{{ route('users.index') }}" class="btn btn-secondary">返回</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 